package com.hadwinling.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hadwinling.blog.entity.Money;
import com.hadwinling.blog.entity.Moneytag;
import com.hadwinling.blog.entity.Tag;
import com.hadwinling.blog.service.AdminService;
import com.hadwinling.blog.service.MoneyService;
import com.hadwinling.blog.service.MoneytagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.hadwinling.blog.controller
 * @date 2020/8/29 16:27
 */
@Controller
public class MoneyController {
    @Autowired
    private MoneyService moneyService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private MoneytagService moneytagService;

    @Value("${admin.article.page.pageSize}")
    private int pageSize;
    @Value("${page.navigatePage}")
    private int navigatePage;

    /**
     * 跳转到日常页面
     * @param model
     * @param pageNum
     * @return
     */
    @GetMapping("/admin/moneyTag")
    public String money(Model model, @RequestParam(name = "pageNum",defaultValue = "1") int pageNum){
        //分页
        PageHelper.startPage(pageNum,pageSize);
        List<Moneytag> moneytagList = moneytagService.selectAllByState(1);
        PageInfo<Moneytag> pageInfo = new PageInfo<>(moneytagList,navigatePage);
        //当前页码
        int currPage = pageInfo.getPageNum();
        //总记录
        Long total = pageInfo.getTotal();
        //总页数
        int totalPage = pageInfo.getPages();
        //所有页面
        int [] pages = pageInfo.getNavigatepageNums();
        //保存分页信息
        model.addAttribute("currPage",currPage);
        model.addAttribute("total",total);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pages",pages);
        model.addAttribute("moneytagList",moneytagList);

        return "AdminCenter/moneyTag";
    }


    /**
     * 跳转到添加记账类别界面
     * @return
     */
    @GetMapping("/admin/addMoneyTag")
    public String toAddMoneyPage(){
        return "AdminCenter/moneyTagInput";
    }

    /**
     * 执行添加记账类别操作
     * @param moneytag
     * @return
     */
    @PostMapping("/admin/addMoneyTag")
    public String addMoneyTag(Moneytag moneytag){
        moneytag.setState(1);
        moneytagService.insertSelective(moneytag);
        return "redirect:/admin/moneyTag";
    }

    /**
     * 执行删除记账类别操作
     * @param moneytagid
     * @return
     *//*
    @PostMapping("/admin/deleteMoneyTag")
    @ResponseBody
    public String deleteTag(@RequestParam(name = "moneytagid") int moneytagid){

        return "1";

    }*/
}
